//
//  PopoverAnimator.swift
//  weibo1
//
//  Created by changhongliang on 2017/7/24.
//  Copyright © 2017年 richinfo. All rights reserved.
//

import UIKit

///在类的外面写的常量或者变量就是全局能够访问的
let PopoverAnimatorShowNotification = "PopoverAnimatorShowNotification"
let PopoverAnimatorDismissNotification = "PopoverAnimatorDismissNotification"
class PopoverAnimator: NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning {
    var isPresent:Bool = false
    
    //返回负责转场的控制器对象
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        return PopoverPresentationController(presentedViewController: presented, presenting: presenting)
    }
    
    /// 返回负责moda动画对象
    ///
    
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        let notificationName = Notification.Name(PopoverAnimatorShowNotification)
        NotificationCenter.default.post(name: notificationName, object: self)
        isPresent = true
        return self
    }
    
    ///返回负责dismiss动画对象
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        let notificationName = Notification.Name(PopoverAnimatorDismissNotification)
        NotificationCenter.default.post(name: notificationName, object: self)
        isPresent = false
        return self
    }
    
    /// 返回动画时长
    ///
    /// - Parameter transitionContext: 转场上下文，提供转场需要的参数
    /// - Returns: 动画时长
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval{
        
        return 0.5
    }
    
    
    /// 转场动画实现函数，一旦实现这个方法默认的转场动画会失效，一切由程序员提供
    ///
    /// - Parameter transitionContext: 转场上下文，提供转场需要的参数
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning){
        //1.获取视图控制器
        //        let toVC = transitionContext.viewController(forKey:UITransitionContextViewControllerKey.to)
        //        let fromVC = transitionContext.viewController(forKey:UITransitionContextViewControllerKey.from)
        if isPresent {
            //2.将toVC 添加到容器视图上
            let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)
            transitionContext.containerView.addSubview(toView!)
            //3.实现动画
            //3.1高度缩为0
            toView?.transform = CGAffineTransform(scaleX: 1.0, y: 0.0)
            //设置锚点
            toView?.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            UIView.animate(withDuration: 0.5, animations: {
                //3.2还原动画
                toView?.transform = CGAffineTransform.identity
            }, completion: { (_) in
                transitionContext.completeTransition(true)
            })
            
        }else{
            // 2.获得fromVC
            let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)
            // 3.实现动画
            UIView.animate(withDuration: 0.2, animations: {
                fromView?.transform = CGAffineTransform(scaleX: 1.0, y: 0.000001)
            }, completion: { (_) in
                //移除视图
                fromView?.removeFromSuperview()
                //一定要告诉系统转场动画结束
                transitionContext.completeTransition(true)
            })
        }
    }
    

    
    
    

}
